Skip to content

fix: coerce by_alias to bool in model_dump to prevent TypeError with pydantic v2#2924

Open
carlosinfantes wants to merge 2 commits intoopenai:mainfrom
carlosinfantes:fix/model-dump-by-alias-none
Open

fix: coerce by_alias to bool in model_dump to prevent TypeError with pydantic v2#2924
carlosinfantes wants to merge 2 commits intoopenai:mainfrom
carlosinfantes:fix/model-dump-by-alias-none

Conversation

@carlosinfantes
Copy link

Summary

model_dump() in openai/_compat.py passes by_alias=None to pydantic's model_dump(), which causes pydantic-core's Rust serializer to raise TypeError: argument 'by_alias': 'NoneType' object cannot be converted to 'PyBool'.

This crashes any API call when the openai logger has DEBUG level enabled, because _build_request() calls model_dump(options, exclude_unset=True) without passing by_alias, so it defaults to None.

Fix

Coerce by_alias to bool before passing to pydantic's model_dump():

# Before
by_alias=by_alias,

# After
by_alias=bool(by_alias),

bool(None) evaluates to False, which preserves the expected default behavior.

File changed

  • src/openai/_compat.py (line 152)

Fixes #2921

Carlos Infantes added 2 commits March 3, 2026 20:05
…emory"

The SDK declares prompt_cache_retention with Literal["in-memory", "24h"]
(hyphen), but the API rejects "in-memory" with a 400 and only accepts
"in_memory" (underscore).

Fixes openai#2883
…pydantic v2

model_dump() passes by_alias=None to pydantic's model_dump(), which causes
pydantic-core's Rust serializer to raise TypeError since it does not accept
None for the by_alias parameter. This crashes any API call when the openai
logger has DEBUG level enabled.

Fixes openai#2921
@carlosinfantes carlosinfantes requested a review from a team as a code owner March 3, 2026 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: by_alias: NoneType in openai-python v2.24.0 when logging.DEBUG is enabled

1 participant